<?php
namespace app\admin\controller;

use app\model\admin;
use think\Request;
use think\Session;
use think\Controller;

class Users extends Controller
{

    //获取会员列表
    public function index()
    {
        $dao = new \app\model\admin\Users();
        $where=[];
        $lists = $dao->get_users_lists($where,$sort='id desc',$paginate=10);
        $this->assign('lists', $lists);
        return $this->fetch('users/index');
    }

    //查询会员详情
    public function detail()
    {
        $dao = new \app\model\admin\Users();
        $id = $request = $this->request->get('id');
        if(empty($id)){
            $data = array();
        }else{
            $data = $dao->users_detail($id);
        }
        return $this->fetch('users/detail',$data);
    }

    //会员添加修改
    public function users_save()
    {
        $dao = new \app\model\admin\Users();
        $data['id'] = deletehtml($this->request->post('id'));
        $data['name'] = deletehtml($this->request->post('name'));
        $data['phone'] = deletehtml($this->request->post('phone'));
        $data['password'] = deletehtml($this->request->post('password'));
        $file = $this->request->file('image');
        if(!empty($file)){//如果图片不为空则为修改
            $updimg_status = upload_file($file,'jpg,png,gif','512000','/uploads/user_img');
            if($updimg_status){ //如果图片上传成功，则把图片路径写到数据库里
                $data['head_url'] = $updimg_status;
            }
        }
        if(empty($data['id'])){
            $data['add_time'] = time();
            $users_status = $dao->add_users($data);
        }else{
            $where['id'] = $data['id'];
            $users_status = $dao->update_users($where,$data);
        }
        if($users_status){
            $this->redirect('/admin/users/index');
        }else{
            $this->error('操作失败！', '/admin/users/detail?id='.$data['id']);
        }
    }

    //改变会员状态
    public function users_status()
    {
        $dao = new \app\model\admin\Users();
        if($this->request->get("id")<1){
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }else{
            $id = $this->request->get("id");
        }
        $status = $this->request->get("status");
        if(!in_array($status,[1,2]))
        {
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }
        $where['id'] = $id;
        $data['status'] = $status;
        $status = $dao->update_users($where,$data);
        if($status > 0){
            return json(['status'=>0,'data'=>(object)[],'msg'=>'操作成功!']);
        }else{
            return json(['status'=>3,'data'=>(object)[],'msg'=>'操作失败!']);
        }
    }

    //删除会员
    public function users_delete()
    {
        $dao = new \app\model\admin\Users();
        if($this->request->get("id")<1){
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }else{
            $id = $this->request->get("id");
        }
        $users_delete = $dao->delete_users([$id]);
        if($users_delete == 0){
            return json(['status'=>3,'data'=>(object)[],'msg'=>'删除失败!']);
        }else{
            return json(['status'=>0,'data'=>$users_delete,'msg'=>'操作成功!']);
        }
    }
}


